package com.jsgt.greekfire.ui.mine.user.messagelist.message

import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.DateFormatUtil
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.MessageBean
import kotlinx.android.synthetic.main.activity_message.*

/**
 * 通知中心
 * author fhx
 * create time 2020/5/11 15:01
 **/
class MessageActivity : BaseActivity() {
    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int = R.layout.activity_message

    companion object {
        private const val MESSAGE_BEAN = "bean"
        fun start(context: Context, bean: MessageBean) {
            val intent = Intent(context, MessageActivity::class.java)
            intent.putExtra(MESSAGE_BEAN, bean)
            context.startActivity(intent)
        }
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        val messageBean = intent.getParcelableExtra<MessageBean>(MESSAGE_BEAN)
        if (messageBean == null){
            finish()
        }

        title = getString(R.string.details)
        msg_title_tv.text = messageBean!!.title
        date_tv.text = DateFormatUtil.formatNew(messageBean.createTime, "yyyy/MM/dd HH:mm")
        val webSettings = msg_webview.settings
        webSettings.domStorageEnabled = true//主要是这句
//        webSettings.javaScriptEnabled = true;//启用js
        webSettings.blockNetworkImage = false//解决图片不显示
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.loadsImagesAutomatically = true
        msg_webview.webChromeClient = WebChromeClient()
        if (messageBean.content!!.startsWith("http")) {
            content_tv.visibility = View.GONE
            msg_webview.loadUrl(messageBean.content!!)
            msg_webview.webViewClient = object : WebViewClient() {

                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    view?.loadUrl(url)
                    return true
                }
            }
            msg_webview.visibility = View.VISIBLE
        } else {
            content_tv.text = messageBean.content!!
        }
    }
}